var express = require("express");
var router = express.Router();

const housesModel = require("../models/houses_module");

// 公用获取房源数据
router.get("/list", function (req, res, next) {
  housesModel.find({}, (err, doc) => {
    if (err) throw err;
    res.json({
      status: 0,
      result: doc,
    });
  });
});

// 查找首页数据 -- (上拉加载下拉刷新)
router.post("/indexdata", function (req, res, next) {
  const { pageSize, currentPage } = req.body;
  // 拿到总共的条数
  housesModel.count({}, (err, count) => {
    if (err) throw err;
    // 如果要做复杂操作，支持使用exec方法
    housesModel
      .find({})
      .skip(+pageSize * (+currentPage - 1))
      .limit(+pageSize)
      .exec((err, doc) => {
        if (err) throw err;
        res.json({
          status: 0,
          list: doc,
          count,
        });
      });
  });
});

// 详情页+心愿单数据接口
router.get("/nearby", function (req, res, next) {
  housesModel
    .find({})
    .skip(3)
    .limit(12)
    .exec((err, doc) => {
      if (err) throw err;
      res.json({
        status: 0,
        list: doc,
      });
    });
});

// 详情页查看全部房源 -- 上拉加载下拉刷新效果
router.post("/all", function (req, res, next) {
  const { page } = req.body;
  // 拿到总共的条数
  housesModel.count({}, (err, count) => {
    count -= 15;
    if (err) throw err;
    // 如果要做复杂操作，支持使用exec方法
    housesModel
      .find({})
      .skip(15 + 8 * (+page - 1))
      .limit(8)
      .exec((err, doc) => {
        if (err) throw err;
        res.json({
          status: 0,
          list: doc,
          count,
        });
      });
  });
});

// 房源排序接口 -- (升序排序)
router.get("/asc", function (req, res, next) {
  housesModel
    .find({})
    .sort({ price: 1 })
    .exec((err, doc) => {
      if (err) throw err;
      res.json({
        status: 0,
        list: doc,
      });
    });
});

// 房源排序接口 -- (降序排序)
router.get("/desc", function (req, res, next) {
  housesModel
    .find({})
    .sort({ price: -1 })
    .exec((err, doc) => {
      if (err) throw err;
      res.json({
        status: 0,
        list: doc,
      });
    });
});

// 房源筛选接口 --
router.post("/choice", function (req, res, next) {
  const { route, region } = req.body;
  housesModel.find(
    { $and: [{ route: route }, { region: region }] },
    (err, doc) => {
      if (err) throw err;
      res.json({
        status: 0,
        list: doc,
      });
    }
  );
});

// 根据路线进行房源筛选
router.post("/route", function (req, res, next) {
  const { route } = req.body;
  housesModel.find({ route: route }, (err, doc) => {
    if (err) throw err;
    res.json({
      status: 0,
      list: doc,
    });
  });
});

// 根据地域进行房源筛选
router.post("/region", function (req, res, next) {
  const { region } = req.body;
  housesModel.find({ region: region }, (err, doc) => {
    if (err) throw err;
    res.json({
      status: 0,
      list: doc,
    });
  });
});

// 根据id查询对应房源所有数据
router.post("/one", function (req, res, next) {
  const { id } = req.body;
  housesModel.findOne({ house_id: id }, (err, doc) => {
    if (err) throw err;
    res.json({
      status: 0,
      list: doc,
    });
  });
});

module.exports = router;
